"use strict";
let prefixs = {};
let funcs = {
  get: [],
  post: [],
  patch: [],
  del: [],
  options: [],
  put: [],
  all: [],
};
class R {
  constructor() {}
  //获取类当中的方法
  static getAction(classObj, func) {
    const action = async function (...args) {
      return new classObj(this)[func](...args);
    };
    return action;
  }
  //注册路由
  static init(app, ctx) {
    for (let prefix in prefixs) {
      const p = prefixs[prefix];
      app.service[p.className] = new p.class(ctx);
      p.funcs.forEach((f) => {
        const tmp = {
          path: prefix + "/" + f,
          class: p.class,
          className: p.className,
          func: f,
          method: f == "info" ? "get" : "post",
        };
        app.router.push(tmp);
        // console.log(`注册路由=>方法:${tmp.method}, 地址:${tmp.path}`);
      });
    }
    for (let m in funcs) {
      funcs[m].forEach((r) => {
        r.pathNew = r.path;
        //如果是/开头，去匹配他的前缀
        if (r.pathNew.indexOf("/") == 0) {
          for (let p in prefixs) {
            if (prefixs[p].className == r.className) {
              r.pathNew = p + r.path;
              break;
            }
          }
        } else {
          r.pathNew = "/" + r.pathNew;
        }
        const tmp = {
          path: r.pathNew,
          class: r.class,
          className: r.className,
          func: r.func,
          method: m,
        };
        app.router.push(tmp);
        // console.log(`注册路由=>方法:${tmp.method}, 地址:${tmp.path}`);
      });
    }
  }
  static get(path) {
    return function (targetClass, func) {
      funcs.get.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static options(path) {
    return function (targetClass, func) {
      funcs.get.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static post(path) {
    return function (targetClass, func) {
      funcs.post.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static put(path) {
    return function (targetClass, func) {
      funcs.put.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static all(path) {
    return function (targetClass, func) {
      funcs.all.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static del(path) {
    return function (targetClass, func) {
      funcs.del.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  static patch(path) {
    return function (targetClass, func) {
      funcs.patch.push({
        className: targetClass.constructor.name,
        class: targetClass.constructor,
        path,
        func,
      });
      return;
    };
  }
  //类注册
  static router(pathPrefix, funcs) {
    return function (targetClass) {
      if (prefixs[pathPrefix]) {
        throw new Error("class name repeat ： " + targetClass.name);
      }
      let obj = {
        funcs: funcs || [],
        className: targetClass.name,
        class: targetClass,
      };
      prefixs[pathPrefix] = obj;
    };
  }
}
/** 暴露注册路由方法 */

/** 暴露实例的prefix和http的各个方法 */
module.exports = R;
